import {
  query as queryUsers,
  peoplelist,
  actionlist,
  creatpeople,
  removepeople,
  peopleDetail,
  updatePeople,
  queryPeople,
  exportOperatorList,
  queryAction,
  peopleactiondetail,
  updateAction,
  removepeopleaction
} from '../services/people';

export default {
  namespace: 'people',

  state: {
    list: [],
    currentPeople: [],
    currentDetail: {},
    currentAction:[],
    currentactionDetail:{}
  },

  effects: {
    //人员信息列表
    *getPeoplelist(_, { call, put }) {
      const response = yield call(peoplelist);
      yield put({
        type: 'saveCurrentPeople',
        payload: response,
      });
    },
    //用户表单关键字查询
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryPeople, payload);
      yield put({
        type: 'saveCurrentPeople',
        payload: response,
      });
    },

    //增加人员信息
    *create({ payload: values }, { call, put }) {
      const response = yield call(creatpeople, values);
      console.log(response);
      yield put({ type: 'getPeoplelist' });
      /*       if (response!== '') {
        reloadAuthorized();
        yield put(routerRedux.push('/people/peoplelist'));
      } */
    },
    //删除人员信息
    *remove({ payload: id }, { call, put }) {
      yield call(removepeople, id);
      yield put({ type: 'getPeoplelist' });
    },
    //查详情信息
    *peopledetail({ payload: id }, { call, put }) {
      const response = yield call(peopleDetail, id);
      yield put({
        type: 'saveDetailPeople',
        payload: response,
      });
    },

    //修改人员信息
    *update({ payload: { id, values } }, { call, put }) {
      yield call(updatePeople, id, values);
      yield put({ type: 'getPeoplelist' });
    },
    //用户权限信息列表
    *getActionlist(_, { call, put }) {
      const response = yield call(actionlist);
      yield put({
        type: 'saveCurrentAction',
        payload: response,
      });
    },
    //用户权限信息列表关键字查询
    *queryaction({ payload }, { call, put }) {
      const response = yield call(queryAction, payload);
      yield put({
        type: 'saveCurrentAction',
        payload: response,
      });
    },
    //查看读者用户权限详情信息
    *peopleactiondetail({ payload: id }, { call, put }) {
      const response = yield call(peopleactiondetail, id);
      yield put({
        type: 'saveDetailPeopleaction',
        payload: response,
      });
    },
    //修改人员信息
    *actionUpdate({ payload: { id, values } }, { call, put }) {
      yield call(updateAction, id, values);
      yield put({ type: 'getActionlist' });
  },
    //删除人员信息
    *removeaction({ payload: id }, { call, put }) {
      yield call(removepeopleaction, id);
      yield put({ type: 'getActionlist' });
    },
  },

  reducers: {
    /*     save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    }, */
    saveCurrentPeople(state, action) {
      return {
        ...state,
        currentPeople: action.payload,
      };
    },
    saveCurrentAction(state, action) {
      return {
        ...state,
        currentAction: action.payload,
      };
    },
    saveDetailPeople(state, action) {
      return {
        ...state,
        currentDetail: action.payload || {},
      };
    },
    saveDetailPeopleaction(state, action) {
      return {
        ...state,
        currentactionDetail: action.payload || {},
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentPeople: {
          ...state.currentPeople,
          notifyCount: action.payload,
        },
        currentDetail: {
          ...state.currentDetail,
          notifyCount: action.payload,  
        },
        currentAction: {
          ...state.currentAction,
          notifyCount: action.payload,  
        },
        currentactionDetail: {
          ...state.currentactionDetail,
          notifyCount: action.payload,  
        },
      };
    },
  },
};
